#include "button_win.h"
#include "ui_button_win.h"


#include <QFileDialog>
#include <QProcess>

Button_Win::Button_Win(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Button_Win)
{
    ui->setupUi(this);
}



Button_Win::~Button_Win()
{
    delete ui;
}

QIcon Button_Win::GetIcon(QString const& fileName, int index)
{
    HICON hicon;
    if(ExtractIconEx(fileName.toStdWString().c_str(), index, &hicon, 0, 1) > 0)
    {
        if(hicon)
            return QIcon(QtWin::fromHICON(hicon));
    }
    return QIcon();
}

//添加软件信息
void Button_Win::nullbutt_tool_fun(MButton *butt)
{
    liststr str_list;
    // 使用QFileDialog打开文件选择对话框
    QString fileName = QFileDialog::getOpenFileName(this, "Open File", "", "软件 (*.exe)");

    if (!fileName.isEmpty()) {
        // 更新标签以显示选中的文件路径
        QFileInfo fileInfo(fileName);

        //设置图片大小
        butt->setIconSize(QSize(50,50));
        //设置字体大小
        QFont font;
        font.setPointSize(12);
        butt->setFont(font);

        butt->set_butt_Information(TOOL,fileInfo.fileName(),fileName,GetIcon(fileName,0));

        str_list.name = fileInfo.fileName();
        str_list.path = fileName;
        list.append(str_list);

        butt->set_isnull(false);

        addbutton("+");
    } else {
        // 用户取消了文件选择

    }
}

//点击按钮设置网站信息
void Button_Win::nullbutt_fun(MButton *butt)
{
    ADD_HTTP *http = new ADD_HTTP;
    liststr str_list;
    http->setWindowTitle("网址设置");
    http->set_button(butt);
    http->exec();

    if(butt->get_isnull() == false)
    {
        //设置图片大小
        butt->setIconSize(QSize(50,50));
        //设置字体大小
        QFont font;
        font.setPointSize(12);
        butt->setFont(font);
        //根据网址设置界面的设置信息设置按钮
        if(butt->get_iconpath() == "")
        {
            butt->set_butt_Information(HTTP,butt->get_filename(),butt->get_filepath());

            str_list.icon = "";
        }else{
            butt->set_butt_Information(HTTP,butt->get_filename(),butt->get_filepath(),QIcon(butt->get_iconpath()));

            str_list.icon = butt->get_iconpath();
        }

        str_list.name = butt->get_filename();
        str_list.path = butt->get_filepath();

        list.append(str_list);

        addbutton("+");
    }
}

void Button_Win::set_button(MButton *butt,liststr str)
{
    //设置图片大小
    butt->setIconSize(QSize(50,50));
    //设置字体大小
    QFont font;
    font.setPointSize(12);
    butt->setFont(font);

    if(win_name == "tool")
    {

        QFileInfo fileInfo(str.path);

        butt->set_butt_Information(TOOL,fileInfo.fileName(),str.path,GetIcon(str.path,0));

        butt->set_isnull(false);
    }else{
        //根据网址设置界面的设置信息设置按钮
        if(str.icon == "")
        {
            butt->set_butt_Information(HTTP,str.name,str.path);
        }else{
            butt->set_butt_Information(HTTP,str.name,str.path,QIcon(str.icon));
        }
    }
}

//运行软件
void Button_Win::run_exe(QString filefath)
{
    QProcess process(this);
    QString str = "\"" + filefath + "\"";
    process.startDetached(str);
}

//打开网页
void Button_Win::open_http(QString httppath)
{
    QDesktopServices::openUrl(QUrl(httppath));
}

//点击按钮的槽函数
void Button_Win::button_slot(MButton *butt)
{
    if(win_name == "tool")
    {
        if(butt->get_isnull() == true)
        {
            nullbutt_tool_fun(butt);
        }else{
            run_exe(butt->get_filepath());
        }
    }else{
        if(butt->get_isnull() == true)
        {
            nullbutt_fun(butt);
        }else{
            open_http(butt->get_filepath());
        }
    }
}

void Button_Win::right_button_slot(MButton *butt)
{
    if(butt->get_isnull() == true)
    {
        return;
    }

    if(QMessageBox::question(this,tr("提示框"),tr("是否确定要删除"),
                         QMessageBox::Yes | QMessageBox::No,QMessageBox::Yes) == QMessageBox::No)
    {
        return;
    }

    for(int i = 0;i < list.size();++i)
    {
        if(list.at(i).path == butt->get_filepath())
        {
            list.removeAt(i);
            delete butt;
            return;
        }
    }
}

//在窗口中添加一个按钮
MButton * Button_Win::addbutton(QString text)
{
    // 去除布局中的弹簧
    if (mainLayout->count() > 0) {
        delete mainLayout->takeAt(mainLayout->count() - 1);
    }

    MButton *button = new MButton(this);
    button->setText(text);
    button->setFixedHeight(60);
    mainLayout->addWidget(button);

    topSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
    mainLayout->addItem(topSpacer);

    connect(button,SIGNAL(clicked_button(MButton *)),this,SLOT(button_slot(MButton *)));
    connect(button,SIGNAL(right_clicked_button(MButton *)),this,SLOT(right_button_slot(MButton *)));

    return button;
}

void Button_Win::set_list(QList<liststr> list_p)
{
    list = list_p;
}

QList<liststr> Button_Win::get_list()
{
    return list;
}

void Button_Win::set_winname(QString name)
{
    win_name = name;
}

QString Button_Win::get_winname()
{
    return win_name;
}

//初始化界面布局
void Button_Win::Button_Win_init()
{
    MButton *button = nullptr;
    mainLayout = new QVBoxLayout;

    //添加弹簧
    if((mainLayout != nullptr) && (mainLayout->count() > 0))
    {
        topSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
        mainLayout->addItem(topSpacer);
    }

    //将布局放到界面中
    this->setLayout(mainLayout);

    //按照列表中的信息配置按钮
    if(list.size() != 0)
    {
        for(int i = 0;i < list.size();++i)
        {
            button = addbutton("");
            set_button(button,list.at(i));
            button->set_isnull(false);
        }
    }

    addbutton("+");
}
